extends Node

export(int) var numberToPlay = 2
export(bool) var enablePitchRandomization = true
export(float) var minPitchScale = .9
export(float) var maxPitchScale = 1.1

var rng = RandomNumberGenerator.new()

func _ready():
	rng.randomize()

func play():
	var validNodes = []	# 每次都选出目前 **所有的** 不是正在播放的音频进行播放
	
	for streamPlayer in get_children():
		if (!streamPlayer.playing):
			validNodes.append(streamPlayer)
	
	for i in numberToPlay:
		# 找出三个有效的声音进行播放
		if (validNodes.size() == 0):
			break
		# 找不到就直接退出
		
		var idx = rng.randi_range(0, validNodes.size() - 1)
		# 随机选择一个不是正在播放的音效
		
		if (enablePitchRandomization):
			validNodes[idx].pitch_scale = rng.	randf_range(minPitchScale, maxPitchScale)
		# 创建随机的音高
		
		validNodes[idx].play()
		validNodes.remove(idx)
		# 播放并设置为无效的音频以防止重复播放
